import Utils ((&&&), tok)
import Data.Char (isDigit)
import Data.List (group)


-- #.#.### 1,1,3
parseLine :: String -> (String, [Int])
parseLine = 
    (&&&) string constraints
    where
        string      = takeWhile (/= ' ')
        constraints = map read . tok "," . dropWhile (not . isDigit)

isValid :: [Int] -> [Char] -> Bool
isValid is cs =
    is == counts
    where
        counts = map length . filter ((=='#') . head) . group $ cs

genSols :: String -> [String]
genSols [] = [[]]
genSols (c:cs)
    | c == '?'  = map ('.':) (genSols cs) ++ map ('#':) (genSols cs)
    | otherwise = map (c:) (genSols cs)

countValids :: ([Char], [Int]) -> Int
countValids (cs, is) = length . filter (isValid is) . genSols $ cs

main = do
    contents <- readFile "input.txt"

    print $ sum . map countValids . map parseLine . lines $ contents

    